package list;

/**
  * 快慢指针法
  */
class 旋转链表_61{
            public ListNode rotateRight(ListNode head, int k) {
                if(head == null || k == 0) return head;
                ListNode fast = head, slow = head;
                for(int i = 0; i < k; i++) {
                    fast = fast.next;
                    if(fast == null) {
                        fast = head;  // 快指针 fast 对 k 取模
                    }
                }
                while(fast.next != null) {
                    fast = fast.next;
                    slow = slow.next;
                }
                fast.next = head;
                head = slow.next;
                slow.next = null;
                return head;
            }
        }

        
